Remote Repositories
Remote Name
A remote name likde origin is an alias for a remote repository's URL.
Instead of typing the full remote URL every time, Git allows you to use a short name with easier reference.
Without a Remote Name
git push https://github.com/user/repo.git main
With a Remote Name
git push origin main
Remote Commands
- List all remote repo of local repo:
git remote -v
It return both fetch and push URLs.
- Fetch URL: Used for pulling (downloading) changes from the remote repository.
- Push URL: Used for pushing (uploading) changes to the remote repository.
By default, both are the same, but they can be different. For example, in some workflows:
- You might fetch from a read-only URL (like
git://or SSH with limited access). - You might push to a different URL (like HTTPS with authentication).
Setting different URL:
--pushflag is used to set a different push URL from the fetch URL
git remote set-url --push origin git@github.com:user/repo.git
- the default url is fetch url
git remote set-url origin https://github.com/user/repo.git
git remote add <name> <url>- Add a new remote repositorygit remote remove <name>- Remove remote repositorygit remote rename <old-name> <new-name>- Rename remote repositorygit remote show <name>- Show information about remote repositorygit remote set-url <name> <new-url>- Change remote URLgit remote get-url <name>- Verify the remote URL
push Commands
It uploads your local commits to a remote repository. It syncs your local branch with a remote branch, allowing others to see and collaborate on your changes.
What Happens Under the Hood?
When you push:
- Git checks your local commits.
- It sends the changes to the remote repository.
- If successful, the remote branch updates to match your local branch.
If your remote branch has new commits, Git prevents you from pushing — you’ll need to pull and resolve conflicts first.
git push- Pushes the changes from the current branch to its corresponding upstream branch.git push <remote> <branch>- pushes the specified branch to the remote.git push -u <remote> <branch>- Pushes the branch and sets it as the upstream branch for futuregit pushandgit pullcommands.git push --force- Forces Git to push changes, even if it overwrites changes on the remote repository.git push --force-with-lease- Similar to--force, but only forces the push if no one else has updated the remote branch since your last fetch. This prevents accidental overwriting of others' work.git push --dry-run- Simulate Push Without Making Changes. Useful for verification before executing a real push.git push --delete <remote> <branch>- Remove a remote branch
fetch Commands
It downloads changes from the remote repository, but it doesn’t update your local working directory or current branch. It only updates your remote-tracking branches, allowing you to inspect the changes before integrating them.
It is used to retrieve the latest changes from a remote repository without merging them into your local repository. It allows you to update your local copy with new branches, tags, and commits available on the remote without modifying your working directory.
git fetch- fetch from default remote, download commit, branches, tags but does not merge them into current branch.git fetch <remote>- fetch from specified remote.git fetch <remote> <branch>- fetch from specified branch.git fetch -all- update all configured remote when a repo has multiple remotesgit fetch --dry-run- Simulates fetching without actually downloading anything.git fetch origin <commit-hash>- Fetches a specific commit from the remote.
pull Commands
It is used to fetch and merge changes from a remote repository into the current branch. It is essentially a combination of git fetch (downloads changes) and git merge (applies them to your branch).
git pull- Fetches and merges changes from the default remote.git pull <remote>- Fetches and merges changes from specified remote.git pull <remote> <branch>- Pull form specified branch.git pull --rebase- Instead of merging changes, this command applies changes on top of your current branch, keeping the history clean.git pull --no-commit- Pulls changes but does not automatically create a merge commit.git pull --no-merge- Fetches updates but does not merge them automatically.
clone Commands
It create a copy of a remote repository on your local machine.
When you clone a repository, Git performs the following actions:
-
Copies the entire repository (all branches, commits, history, etc.) from the remote server to your local machine.
-
Sets up the remote connection (origin by default) so you can fetch, push, and pull changes.
git clone <repository_url> <new_directory_name>- Clone the repo into a specific directory.git clone --branch <branch_name> <repository_url>- Clone a specific branch.git clone --depth 1 <repository_url>- If you only need the latest version of the repository and not the full commit history.